package com.gollum.manager.factory;

import com.gollum.common.enums.Operator;
import com.gollum.common.utils.ServletUtils;
import com.gollum.common.utils.SpringUtils;
import com.gollum.common.utils.ip.AddressUtils;
import com.gollum.common.utils.ip.IpUtils;
import com.gollum.system.domain.model.SysLogininfor;
import com.gollum.system.domain.model.SysMailLog;
import com.gollum.system.domain.model.SysOperLog;
import com.gollum.system.service.SysLogininforService;
import com.gollum.system.service.SysMailLogService;
import com.gollum.system.service.SysOperLogService;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.TimerTask;

/**
 * 异步保存日志工厂
 *
 * @author Herther
 * @version 1.0.0
 * @createTime 2022年08月26日 23:10:00
 */
public class LogAsyncFactory {

    private static final Logger log = LoggerFactory.getLogger(LogAsyncFactory.class);

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask saveLogininfor(final String username, final String status, final String message, final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append("["+ip+"]");
                s.append(address);
                s.append("["+username+"]");
                s.append("["+status+"]");
                s.append("["+message+"]");
                // 打印信息到日志
                log.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLogininfor logininfor = new SysLogininfor();
                logininfor.setUsername(username);
                logininfor.setIpAddress(ip);
                logininfor.setIpLocation(address);
                logininfor.setOs(os);
                logininfor.setSourceType(browser);
                logininfor.setMessage(message);
                logininfor.setLoginTime(new Date());
                // 日志状态
                if (Operator.SUCCESS.isStatus(status)){
                    logininfor.setStatus(Operator.SUCCESS.getValue());
                } else if (Operator.ERROR.isStatus(status)) {
                    logininfor.setStatus(Operator.ERROR.getValue());
                }else{
                    logininfor.setStatus(Operator.UNKNOWN.getValue());
                }
                //保存
                SpringUtils.getBean(SysLogininforService.class).save(logininfor);
            }
        };
    }


    /**
     * 保存操作日志
     *
     * @param operLog 操作日志实体
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/15 23:08
     */
    public static TimerTask saveOperator(SysOperLog operLog) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(SysOperLogService.class).save(operLog);
                log.info("保存操作日志状态：[SUCCESS]");
            }
        };
    }

    /**
     * 保存邮箱发送日志
     *
     * @param
     * @return
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/11/6 18:30
     */
    public static TimerTask saveSendMallLog(SysMailLog sysMailLog){
        return new TimerTask() {
            @Override
            public void run() {
                SpringUtils.getBean(SysMailLogService.class).save(sysMailLog);
                log.info("保存邮箱发送日志状态：[SUCCESS]");
            }
        };
    }

}
